<template>
  <!-- 可以放一个加载动画 -->
  <div v-if="loading" class="loading-container">
    <p>Loading...</p>
    <!-- 可以在这里添加加载动画 -->
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import axios from "axios";

const route = useRoute();
const router = useRouter();
const loading = ref(true);

const fetchUserAndRedirect = async () => {
  try {
    const res = await axios.post("http://127.0.0.1:8000/home/reaa_user/", {
      id: route.query.id,
    });

    // 根据用户角色跳转到对应页面
    switch (res.data) {
      case 2:
        router.push({ name: "home" }); // 普通用户跳转到首页
        break;
      case 1:
        router.push({ name: "HrHome" }); // HR用户跳转到HR首页
        break;
      default:
        router.push({ name: "home" }); // 默认跳转到首页
    }
  } catch (error) {
    console.error("Error:", error);
    router.push({ name: "error" }); // 跳转到错误页
  } finally {
    loading.value = false;
  }
};

onMounted(() => {
  fetchUserAndRedirect();
});
</script>

<style scoped>
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
</style>
